import 'package:cloudapp/bus/dtypes.dart';
import 'package:flutter/cupertino.dart';

class UploadList with ChangeNotifier {
  final _left = List<UploadFileInfo>();
  final _finished = List<UploadFileResult>();
  var _syncStatus = SyncStatus.Finished;

  UploadList();

  static UploadList _i;
  factory UploadList.i() {
    if(_i == null) {
      _i = UploadList();
    }

    return _i;
  }

  int get leftLength => _left.length;
  int get finishedCount => _finished.length;
  UploadFileInfo get head {
    if(_left.length > 0) {
      return _left[0];
    }
    return null;
  }
  SyncStatus get status => _syncStatus;

  // 初始化队列
  void initList(List<UploadFileInfo> list) {
    _left.clear();
    _finished.clear();
    _left.addAll(list);
    notifyListeners();
  }

  void finishedUploadOne(UploadFileResult result) {
    var pos = _left.indexWhere((element) => element.localFileId == result.localFileId);
    if(pos == -1) {
      return;
    }

    _left.removeAt(pos);
    _finished.add(result);
    notifyListeners();
  }

  get upStatus => _syncStatus;
  void setSyncPause() {
    _syncStatus = SyncStatus.Paused;
    notifyListeners();
  }

  void setSyncRun() {
    _syncStatus = SyncStatus.Running;
    notifyListeners();
  }

  void setSyncFinished() {
    _syncStatus = SyncStatus.Finished;
    notifyListeners();
  }
}
